package tcln.gxtx.client.widget.grid.renderer.html;

import tcln.gxtx.client.utils.assertion.GwtAssert;
import tcln.gxtx.client.widget.treegrid.AbstractTreeGrid;
import tcln.gxtx.client.widget.treegrid.model.AbstractTreeModel;
import tcln.gxtx.client.widget.treegrid.utils.DefaultTreeGridModelDataFormatter;
import tcln.gxtx.client.widget.treegrid.utils.TreeGridModelDataFormatter;

import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;

/**
 * Abstract implementation of {@link HtmlGridCellRenderer} which relies on {@link TreeGridModelDataFormatter} to format
 * data.
 * 
 * @param <M>
 * @author Truong Chau Lien Ngoc (lienngoc@gmail.com)
 */
public abstract class AbstractHtmlGridCellRenderer<M extends AbstractTreeModel> implements HtmlGridCellRenderer<M> {
    protected static final int DEFAULT_MARGIN_TOP = 3;
    protected static final int DEFAULT_MARGIN_RIGHT = 3;

    protected AbstractTreeGrid<M> ownerTree;
    protected ColumnConfig ownerColumn;
    protected TreeGridModelDataFormatter<M> dataFormatter = new DefaultTreeGridModelDataFormatter<M>();
    protected Margins margins = new Margins(DEFAULT_MARGIN_TOP, DEFAULT_MARGIN_RIGHT, 0, 0);

    /**
     * @param ownerColumn
     */
    public AbstractHtmlGridCellRenderer(ColumnConfig ownerColumn) {
        this.ownerColumn = ownerColumn;
    }

    /**
     * Sets new value for margins.
     * 
     * @param margins
     *            the margins to set.
     */
    public void setMargins(Margins margins) {
        this.margins = margins;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void setOwnerTree(AbstractTreeGrid<M> ownerTree) {
        this.ownerTree = ownerTree;
    }

    /**
     * Sets new value for dataFormatter.
     * 
     * @param dataFormatter
     *            the dataFormatter to set.
     */
    public void setDataFormatter(TreeGridModelDataFormatter<M> dataFormatter) {
        GwtAssert.ARGUMENTS.notNull(dataFormatter, "Data formatter is required");
        this.dataFormatter = dataFormatter;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public final ColumnConfig getOwnerColumn() {
        return ownerColumn;
    }
}
